package com.base.app_core.util.retrofit;

import com.base.app_core.util.AppConstants;
import com.base.app_core.util.log.F;
import com.base.app_core.util.security.Base64Utils;
import com.base.app_core.util.security.RSAUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.RequestBody;
import retrofit2.Converter;

/**
 * Created by zhang on 2016/5/31.
 * <p>
 * 自定义请求RequestBody
 */
public class JsonRequestBodyConverter<T> implements Converter<T, RequestBody> {

    private Gson gson;
    private T adapter;

    public JsonRequestBodyConverter(Gson gson, T adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public RequestBody convert(T value) throws IOException {
        //加密
        String content=new Gson().toJson(value);
        byte[] postBody = null; //对象转化成json
        F.http("请求：" + value.toString());
        try {
            postBody = RSAUtils.encryptByPublicKey(content.getBytes(), AppConstants.publicJavaKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RequestBody.create(AppConstants.MEDIA_TYPE, Base64Utils.encode(postBody));
    }

}
